/*
* @Author: dengjiayao
* @Date:   2017-06-26 09:15:44
 * @Last Modified by: songwh
 * @Last Modified time: 2018-03-27 10:06:10
*/

import moment from 'moment'

import Pages from '../../component/pages/pages.vue'
import Uploader from '../item-uploader/item-uploader.vue'

import { showMsg, getHomeBannerList, deleteHomeBannerItem } from '../../vuex/action'

export default {
  name: 'cast-talk-item-list',
  components: {
    'vue-pages': Pages,
    'vue-uploader': Uploader
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      }
    }
  },
  vuex: {
    getters: {
      fresh: state => state.castTalkNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetlist: getHomeBannerList,
      xDelete: deleteHomeBannerItem
    }
  },
  created() {
    this.getList({})
  },
  methods: {
    getList({start=this.pages.start, limit=this.pages.limit}) {
      this.xGetlist({
        start: start,
        limit: limit
      })
        .then((res) => {
          this.list = res.data.list
          this.pages.totalPages = Math.ceil(res.data.total / this.pages.limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    },
    deleteItem(id) {
      this.xDelete({
        id: id
      })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.msg
          })
        })
    }
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
